package com.origin.app.greendao

import android.util.Log
import com.origin.app.MyApplication
import com.origin.app.bean.ABean
import com.origin.app.bean.TestBean
import com.origin.app.bean.UserProtectionAddressBean
import com.origin.greendaolibrary.GreenDaoManager
import com.origin.utils.log.LogUtils

object AppDaoManager {

    private var daoSession: DaoSession? = null
    private fun loadDao(): DaoSession? {
        if (daoSession == null) {
            val daoMaster = GreenDaoManager.getWritableDaoMaster(
                MyApplication.getInstance(),
                "stealth.db",
                arrayOf(TestBeanDao::class.java, ABeanDao::class.java
                ,UserProtectionAddressBeanDao::class.java
                ),
                GreenDaoOpenHelper::class.java,
                DaoMaster::class.java
            )
            Log.e("当前是否为空==>", "mContext==  AppInfoDao  ....  " + (daoMaster == null))
            daoSession = daoMaster.newSession()
        }

        if (daoSession == null) {
            throw java.lang.Exception("初始化数据库异常")
        }
        return daoSession
    }

    public fun loadList() {
        val list = loadDao()?.testBeanDao?.loadAll()
        list?.forEach {
            LogUtils.__e("内容  ${it.toString()}")
        }
    }

    public fun addBean(testBean: TestBean) {
        loadDao()?.testBeanDao?.insert(testBean)
    }


    public fun loadAList() {
        val list = loadDao()?.aBeanDao?.loadAll()
        list?.forEach {
            LogUtils.__e("内容 ABean  ${it.toString()}")
        }
    }

    public fun addABean(testBean: ABean) {
        loadDao()?.aBeanDao?.insert(testBean)
    }

    public fun loadUserAddressList() {
        val list = loadDao()?.userProtectionAddressBeanDao?.loadAll()
        list?.forEach {
            LogUtils.__e("内容 UserAddress  ${it.toString()}")
        }
    }

    public fun addUserAddressBean(testBean: UserProtectionAddressBean) {
        loadDao()?.userProtectionAddressBeanDao?.insert(testBean)
    }

}